package live.jialing.sip.base.repository;

import live.jialing.core.data.jpa.CustomJpaRepository;
import live.jialing.sip.base.domain.Squad;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * 班
 */
public interface ClassRepository extends CustomJpaRepository<Squad, Long> {

    /**
     * 毕业指定年级中所有的班级
     *
     * @param schoolId 学校ID
     * @param grade    入学年份
     */
    @Modifying(clearAutomatically = true)
    @Query(value = "UPDATE Squad c SET c.status = 'GRADUATE' WHERE c.school.id = ?1 AND c.grade = ?2 AND c.status = 'ONLINE' ")
    void graduate(long schoolId, int grade);

    /**
     * 获取学校所有在读的班
     * @param schoolId 学校id
     * @return
     */
    @Query(value = "select c from Squad c where c.school.id = ?1 AND c.status = 'ONLINE'")
    List<Squad> findBySchool(long schoolId);

    /**
     * 获取学校、年级的所有在读的班
     * @param schoolId
     * @param grade
     * @return
     */
    @Query(value = "select c from Squad c where c.school.id = ?1 and c.grade = ?2 AND c.status = 'ONLINE'")
    List<Squad> findBySchoolAndGrade(long schoolId,int grade);
}